package com.flymobi.mshop.activity;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.flymobi.mshop.R;
import com.flymobi.mshop.po.Order;
import com.flymobi.mshop.util.Constants;
import com.flymobi.mshop.util.service.DilivTypeService;
import com.flymobi.mshop.util.service.PayTypeService;

public class PeiSong extends CommonActivity {
	private LinearLayout zhif;
	private LinearLayout peis;
	private Spinner zffangshi;
	private Spinner psfangshi;
	private TextView spzf;
	private TextView spps;
	private Button btnOK;
	private List<Map<String, Object>> list;
	private String[] zf;
	private List<Map<String, Object>> pslist;
	private String[] ps;
	private String[] yunfei;
	private String yf;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.peisong);
		Init();
		zffangshi = new Spinner(PeiSong.this);
		zffangshi.setId(2);
		System.out.println("zhifu:" + zffangshi.getId());
		list = PayTypeService.list();
		zf = new String[list.size()];
		int m = 0;
		for (Map<String, Object> map : list) {
			zf[m] = map.get("name").toString();
			m++;
		}

		ArrayAdapter<String> zfadapter = new ArrayAdapter<String>(PeiSong.this,
				android.R.layout.simple_spinner_item, zf);
		zfadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		zffangshi.setAdapter(zfadapter);
		psfangshi = new Spinner(PeiSong.this);
		psfangshi.setId(3);

		pslist = DilivTypeService.list();
		ps = new String[pslist.size()];
		yunfei = new String[pslist.size()];
		int i = 0;
		for (Map<String, Object> map : pslist) {
			ps[i] = map.get("name").toString();
			yunfei[i] = map.get("cost").toString();
			i++;
		}
		ArrayAdapter<String> psadapter = new ArrayAdapter<String>(PeiSong.this,
				android.R.layout.simple_spinner_item, Constants.DD.SHUZU.ps);
		psadapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		psfangshi.setAdapter(psadapter);
		zhif.addView(zffangshi);
		peis.addView(psfangshi);
		zffangshi.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		psfangshi.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				yf = yunfei[arg2];
				System.out.println("运费:" + yf);
				FillOrder.valueMap.put("yunfei", yf);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		btnOK.setOnClickListener(new OKClick());
	}

	class OKClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Order.setZhifu(spzf.getText().toString());
			Order.setPeisong(spps.getText().toString());
			FillOrder.valueMap.put("zhifu", spzf.getText().toString());
			Intent intent = new Intent();
			intent.setClass(PeiSong.this, FillOrder.class);
			PeiSong.this.startActivity(intent);
		}
	}

	private void Init() {
		zhif = (LinearLayout) findViewById(R.id.payment);
		peis = (LinearLayout) findViewById(R.id.distribution);
		btnOK = (Button) findViewById(R.id.btnOK);
		spzf = new TextView(PeiSong.this);
		spps = new TextView(PeiSong.this);
	}
	// @Override
	// public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
	// long arg3) {
	// // TODO Auto-generated method stub
	// System.out.println("aaa:"+arg1.getId());
	// switch (arg1.getId()) {
	// case 2:
	// //spzf.setText(Constants.DD.SHUZU.zf[arg2]);
	// break;
	// case 3:
	// //spps.setText(Constants.DD.SHUZU.ps[arg2]);
	// yf = yunfei[arg2];
	// System.out.println("运费："+yf);
	// break;
	// default:
	// break;
	// }
	// }
}
